package com.zerosky.crm.service.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.RoleDAO;
import com.zerosky.crm.po.Role;
import com.zerosky.crm.service.RoleService;
import com.zerosky.crm.util.PageInfo;

/**
 * Role业务逻辑实现类
 * 
 * @author hugy;
 * @since 2017-06-09
 *
 */
@Service(value="roleService")
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDAO roleDAO; // Role数据接口

	/**
	 * Role列表
	 * 
	 * @author hugy;
	 * @since 2017-06-09
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Role> getRoleList(Map<String, Object> map) {
		PageInfo<Role> pageRole = new PageInfo<Role>();
		if (map == null) {
			pageRole.setList(roleDAO.getRoleList(null));
		} else {
			Integer pageSize = (Integer) map.get("limit");
			Integer page = (Integer) map.get("page");
			map.put("pageSize", pageSize);
			map.put("pageIndex", (page - 1) * pageSize);

			pageRole.setPageCount(roleDAO.getRoleCount(map));

			pageRole.setList(roleDAO.getRoleList(map));
		}
		return pageRole;
	}

	/**
	 * 通过主键查找Role实体
	 * 
	 * @param id
	 *            Role主键
	 * @author hugy;
	 * @since 2017-06-09
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Role getRoleById(String id) {
		return roleDAO.getRoleById(id);
	}

	/**
	 * 保存Role
	 * 
	 * @param Role
	 *            实体对象, operation 操作类型
	 * @author hugy;
	 * @since 2017-06-09
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void saveRole(Role role, String operation) throws Exception {
		if ("新增".equals(operation)) {
			roleDAO.addRole(role);
		} else if ("修改".equals(operation)) {
			roleDAO.updateRole(role);
		}
	}

	/**
	 * 批量删除Role
	 * 
	 * @param idArray
	 *            Role主键数组
	 * @author hugy;
	 * @since 2017-06-09
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void deleteRoleById(String[] idArray) throws Exception {
		for (String id : idArray) {
			if (StringUtils.isNotEmpty(id)) {
				roleDAO.deleteRoleById(id);
			}
		}
	}
}
